//
//  BaseTableView.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/4/9.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

class CMDBBaseTableView: UITableView {
    
    lazy var tableViewdataArray : Array<AnyObject>? = [AnyObject]()
    
    var emptyView : CMDBBaseView?
    
    var emptyLabel : UILabel?
    
    let showStrFont : UIFont = CMDB_20_Bold_FONT
    
    var cellType : CMDBCellType?
    
    lazy var tableViewAdapterArray : Array<CellAdapter>? = [CellAdapter]()
    
    override init(frame: CGRect, style: UITableViewStyle) {
        
        super.init(frame: frame, style: style)
        
        setupTableView()
        
        tableAddSubViews()
        
        tableBuildSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension CMDBBaseTableView{
    
    func tableViewBuildEmptyView(){
        
        emptyLabel = UILabel(strColor: word54Color, string: "系统暂时没有数据", strFont: showStrFont)
        
        emptyLabel?.center = CGPoint(x: CMDB_Screen_Width * 0.5, y: 80)

        emptyView?.addSubview(emptyLabel!)
        
    }
    
    func updateEmptyMessage(message : String){
        
//        emptyLabel?.text = message
//        
//        let size = message.suitToSize(MaxSize, fontSize:22 , lineSpacing: 1)
//        
//        emptyLabel?.frame.size.width = size.width + 5
//        
//        emptyLabel?.frame.size.height = size.height + 4
        
    }
    
    func setupTableView(){
        
        dataSource = self
        
        delegate = self
        
        showsVerticalScrollIndicator = false
        
        showsHorizontalScrollIndicator = false
        
        bounces = false
        
        separatorStyle = .None
        
        keyboardDismissMode = .OnDrag
        
        emptyView  = CMDBBaseView.getInstance()
        
//        baseListViewDelegate = self
    }
    
    func tableAddSubViews(){
        
    }
    
    func tableBuildSubviews(){
        
    }
    
//    func tableHeaderView(){
//        
//        
//    }
    
    
}

//MARK : process data
extension CMDBBaseTableView{

    func updateCellAdpaters(array: [AnyObject], isInsert: Bool) {
        if !isInsert {
            if self.tableViewdataArray!.count > 0 {
                self.tableViewdataArray!.removeAll()
                self.tableViewdataArray! += array
            }
        }
        else {
            self.tableViewdataArray = array
        }
        if self.tableViewAdapterArray!.count > 0 {
            self.tableViewAdapterArray!.removeAll()
        }
        for i in 0..<array.count {
            let baseAdapter = CustomBaseCell.Adapter(nil, data: array[i], cellHeight: 0, type: 0)
            self.tableViewAdapterArray!.append(baseAdapter)
        }
        reloadData()
    }
    

    // 更新数据
    internal func updateData(data: AnyObject?) {
        
        if let _ = tableViewdataArray {
            
            tableViewdataArray?.removeAll()
        }
        tableViewdataArray! += data as! Array
        
        //        if let _ = mj_header {
        //
        //            mj_header.endRefreshing()
        //        }
        
        if tableViewdataArray?.count == 0 {
            
            if let _ = emptyView {
                
                if let _ = mj_header {
                    
                    mj_header.hidden = true
                }
                
                emptyView?.frame = bounds
                
                emptyView?.mDelegate = self
                
                addSubview(emptyView!)
                
                bringSubviewToFront(emptyView!)
                
                tableViewBuildEmptyView()
                
                emptyView?.hidden = false
                
                bounces = false
            }
            
        } else {
            
            emptyView?.hidden = true
            
            if let _ = mj_header {
                
                mj_header.hidden = false
                
                //                mj_header.endRefreshing()
            }
            
            if tableViewdataArray?.count < 20 {
                if let _ = mj_footer {
                    
                    mj_footer.hidden = true
                }
            }
            reloadData()
        }
    }
    
    internal func updateDataWithOutReload(data: AnyObject?) {
        
        if let _ = tableViewdataArray {
            
            tableViewdataArray?.removeAll()
        }
        tableViewdataArray! += data as! Array
        
        if let _ = mj_header {

            mj_header.endRefreshing()
        }
        
        if tableViewdataArray?.count == 0 {
            
//            if let _ = emptyView {
            
                if let _ = mj_header {
                    
                    mj_header.hidden = true
                }
            
                emptyView?.frame = bounds
                
                emptyView?.mDelegate = self
                
                addSubview(emptyView!)
                
                bringSubviewToFront(emptyView!)
            
                tableViewBuildEmptyView()
                
                emptyView?.hidden = false
                
//                bounces = false
//            }
            
        } else {
            
            emptyView?.hidden = true
            
            if let _ = mj_header {
                
                mj_header.hidden = false
                
                //                mj_header.endRefreshing()
            }
            
            if tableViewdataArray?.count < 20 {
                if let _ = mj_footer {
                    
                    mj_footer.hidden = true
                }
            }
        }
    }

    
    
    // 更新tableHeader
    internal func updateHeaderData(data: AnyObject?) {
        // do nothing
    }
    
    
    
    internal func appendData(data: AnyObject?) {
        
        tableViewdataArray! += data as! Array
        
        reloadData()
    }

    
}



extension CMDBBaseTableView : UITableViewDataSource
{
    
    internal func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    internal func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tableViewdataArray!.count
    }
    
    internal func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell: CustomBaseCell? =  tableView.dequeueReusableCellWithIdentifier("\(cellType!.rawValue)") as? CustomBaseCell
        
        if cell == nil {
            
            cell = CustomBaseCell.tableViewCell(cellType, indexPath: indexPath) as? CustomBaseCell
            
        }
        
        cell?.indexPath = indexPath
        
        cell?.mDelegate = self
        
        cell!.update(tableViewdataArray![indexPath.row])
        
        return cell!
    }
}

extension CMDBBaseTableView : UITableViewDelegate {
    
    internal func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        
        if tableViewdataArray?.count > 0 {
            mDelegate?.onItemClickType!("TableViewCellClick", data: tableViewdataArray![indexPath.row], indexPath: indexPath)
        }
    }
    
    internal func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return CustomBaseCell.HeightWithData(tableViewdataArray![indexPath.row])
    }

}

extension CMDBBaseTableView: CMDBBaseViewDelegate {
    // MARK: item click
    
    override func onItemClickType(itemClickType: String?, data: AnyObject?, indexPath: NSIndexPath?) {
        
        mDelegate?.onItemClickType!(itemClickType, data: data, indexPath: indexPath)
        
    }
}
